﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Form;
using PMPUtils.Goals;

namespace PMP.Goals
{
    public partial class GoalManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);

            if (!IsPostBack)
            {
                txtGoal.Text = Request.Params["GOAL"];
                txtTaskID.Text = Request.Params["TASK"];
            }
        }

        protected void btnViewDetails_Click(object sender, EventArgs e)
        {
            PopupForm ViewGoalDetails = new PopupForm(Response, "ViewGoalDetails.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "ViewGoalDetails2", "1100", "500", "100", "50");
            ViewGoalDetails.Popup();

            Close();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            String goalID = txtGoal.Text.Trim();

            try
            {
                GoalsOperator.RemoveGoal(goalID);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            Messenger.Show(Response, "The goal and its details are all removed.");

            Close();
        }

        protected void btnSwitchOwner_Click(object sender, EventArgs e)
        {
            PopupForm switchOwner = new PopupForm(Response, "GoalManagement_SwitchOwner.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "GoalManagement_SwitchOwner", "350", "150", "400", "200");
            switchOwner.Popup();

            Close();
        }

        protected void btnChangePriority_Click(object sender, EventArgs e)
        {
            PopupForm changePriority = new PopupForm(Response, "GoalManagement_ChangePriority.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "GoalManagement_ChangePriority", "350", "150", "400", "200");
            changePriority.Popup();

            Close();
        }

        protected void btnChangeStatus_Click(object sender, EventArgs e)
        {
            PopupForm changeStatus = new PopupForm(Response, "GoalManagement_ChangeStatus.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "GoalManagement_ChangeStatus", "350", "150", "400", "200");
            changeStatus.Popup();

            Close();
        }

        protected void btnChangeEstimation_Click(object sender, EventArgs e)
        {
            String goalID = txtGoal.Text.Trim();
            String taskID = txtTaskID.Text.Trim();

            // Update the estimation if not bound to a specific task
            if (taskID.Equals(String.Empty))
            {
                PopupForm changeEstimation = new PopupForm(Response, "GoalManagement_ChangeEstimation.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "GoalManagement_ChangeEstimation", "400", "150", "400", "200");
                changeEstimation.Popup();
            }
            else
            {
                Messenger.Show(Response, "Linked to a task, can NOT update the estimation.");
                return;
            }

            Close();
        }

        protected void btnChangeDueDate_Click(object sender, EventArgs e)
        {
            PopupForm changeDueDate = new PopupForm(Response, "GoalManagement_ChangeDueDate.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "GoalManagement_ChangeDueDate", "350", "300", "400", "200");
            changeDueDate.Popup();

            Close();
        }

        protected void btnChangeExepectedDueDate_Click(object sender, EventArgs e)
        {
            PopupForm changeExpectedDueDate = new PopupForm(Response, "GoalManagement_ChangeExpectedDueDate.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "GoalManagement_ChangeExpectedDueDate", "350", "300", "400", "200");
            changeExpectedDueDate.Popup();

            Close();
        }

        private void Close()
        {
            PopupForm thisForm = new PopupForm(Response);
            thisForm.Close();
        }

        protected void btnDetailDesign_Click(object sender, EventArgs e)
        {
            PopupForm detailDesign = new PopupForm(Response, "../Documentation/DetailDesignManagement.aspx?GOAL=" + txtGoal.Text.Trim(), "DetailDesignManagement", "1000", "800", "50", "100");
            detailDesign.Popup();

            Close();
        }

        protected void btnScopeManagement_Click(object sender, EventArgs e)
        {
            PopupForm scopeManagement = new PopupForm(Response, "../Documentation/ScopeManagement.aspx?GOAL=" + txtGoal.Text.Trim(), "ScopeManagement", "1000", "800", "50", "100");
            scopeManagement.Popup();

            Close();
        }

        protected void btnChangeSprint_Click(object sender, EventArgs e)
        {
            PopupForm changeSprint = new PopupForm(Response, "GoalManagement_ChangeSprint.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "GoalManagement_ChangeSprint", "350", "300", "400", "200");
            changeSprint.Popup();

            Close();
        }

        protected void btnChangeGoalDesc_Click(object sender, EventArgs e)
        {
            PopupForm change = new PopupForm(Response, "GoalManagement_ChangeGoalDescription.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "GoalManagement_ChangeGoalDescription", "350", "300", "400", "200");
            change.Popup();

            Close();
        }

        protected void btnScoreboard_Click(object sender, EventArgs e)
        {
            PopupForm scoreboard = new PopupForm(Response, "GoalManagement_Scoreboard.aspx?PARENT_ID=" + txtGoal.Text.Trim(), "GoalManagement_Scoreboard", "1000", "550", "75", "100");
            scoreboard.Popup();

            Close();
        }
    }
}
